package com.chexiehui.jc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.ChooseGridAdapter;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.entity.BaiDuLocationInfo;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.mvp.home.LoanQuotaCalculateCovenant;
import com.chexiehui.jc.mvp.home.LoanQuotaCalculatePresenter;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.CodeCountDownTimer;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.utils.SoftKeyboardFixerForFullscreen;
import com.chexiehui.jc.widget.NonSlidingGridView;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 贷款额度测算
 */
public class LoanQuotaCalculateActivity extends BaseMvpActivity<LoanQuotaCalculatePresenter> implements LoanQuotaCalculateCovenant.View {
    @BindView(R.id.nsgv_income_grid)
    NonSlidingGridView nsgvIncomeGrid;
    @BindView(R.id.nsgv_funds_grid)
    NonSlidingGridView nsgvFundsGrid;
    @BindView(R.id.nsgv_intended_purchase_time_grid)
    NonSlidingGridView nsgvIntendedPurchaseTimeGrid;
    @BindView(R.id.nsgv_looked_car)
    NonSlidingGridView nsgvLookedCar;
    @BindView(R.id.et_person_name)
    EditText etPersonName;
    @BindView(R.id.et_person_phone)
    EditText etPersonPhone;
    @BindView(R.id.et_person_code)
    EditText etPersonCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.tv_person_city)
    TextView tvPersonCity;
    @BindView(R.id.rtv_apply)
    RadiusTextView rtvApply;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_choose_model_name)
    TextView tvChooseModelName;
    @BindView(R.id.ll_choose_city)
    LinearLayout llChooseCity;
    @BindView(R.id.sb_seek_car_toggle)
    SwitchButton sbSeekCarToggle;
    private List<String> incomeChooseData;
    private ChooseGridAdapter incomeAdapter;
    private List<String> fundsChooseData;
    private ChooseGridAdapter fundsAdapter;
    private List<String> purchaseTimeChooseData;
    private ChooseGridAdapter purchaseTimeAdapter;
    private List<String> lookedCarChooseData;
    private ChooseGridAdapter lookedCarAdapter;
    private CarInfo carChooseInfo;
    private String chooseCity = "";
    private int isSeeCarToShop = 1;
    private int personIncome = 0;
    private int personSecurity = 0;
    private int personTime = 0;
    private int personSeeCar = 0;
    private CodeCountDownTimer phoneLoginTimer;

    @BindView(R.id.money_rt)
    RadiusTextView money_rt;

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("贷款额度测算");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_loan_quota_calculate;
    }

    @Override
    protected void initView(Bundle var1) {
        money_rt.setText("?\b?\b0\b0\b6\b8\b7");

        SoftKeyboardFixerForFullscreen.assistActivity(this);
        incomeChooseData = new ArrayList<>();
        incomeChooseData.add("1万元以下");
        incomeChooseData.add("1-2万元");
        incomeChooseData.add("2万元以上");
        nsgvIncomeGrid.setAdapter(incomeAdapter = new ChooseGridAdapter(mContext, incomeChooseData));
        nsgvIncomeGrid.setOnItemClickListener((parent, view, position, id) -> {
            incomeAdapter.changeState(position);
            personIncome = position;
        });

        fundsChooseData = new ArrayList<>();
        fundsChooseData.add("有");
        fundsChooseData.add("无");
        nsgvFundsGrid.setAdapter(fundsAdapter = new ChooseGridAdapter(mContext, fundsChooseData));
        nsgvFundsGrid.setOnItemClickListener((parent, view, position, id) -> fundsAdapter.changeState(position));
        nsgvFundsGrid.setOnItemClickListener((parent, view, position, id) -> {
            fundsAdapter.changeState(position);
            personSecurity = position;
        });

        purchaseTimeChooseData = new ArrayList<>();
        purchaseTimeChooseData.add("30天以内");
        purchaseTimeChooseData.add("60天以内");
        purchaseTimeChooseData.add("其他");
        nsgvIntendedPurchaseTimeGrid.setAdapter(purchaseTimeAdapter = new ChooseGridAdapter(mContext, purchaseTimeChooseData));
        nsgvIntendedPurchaseTimeGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                purchaseTimeAdapter.changeState(position);
                personTime = position;
            }
        });

        lookedCarChooseData = new ArrayList<>();
        lookedCarChooseData.add("是");
        lookedCarChooseData.add("否");
        nsgvLookedCar.setAdapter(lookedCarAdapter = new ChooseGridAdapter(mContext, lookedCarChooseData));
        nsgvLookedCar.setOnItemClickListener((parent, view, position, id) -> {
            personSeeCar = position;
            lookedCarAdapter.changeState(position);
        });
        sbSeekCarToggle.setChecked(true);
        sbSeekCarToggle.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isSeeCarToShop = isChecked ? 1 : 0;
        });
        rtvApply.setEnabled(false);
    }

    @OnClick({R.id.rll_choose_car_model, R.id.tv_get_code, R.id.ll_choose_city,R.id.rl_see_to_shop, R.id.rtv_apply, R.id.tv_agreement})
    public void onViewClicked(View view) {
        if(NoDoubleClickUtils.isDoubleClick()) return;
        switch (view.getId()) {
            case R.id.rll_choose_car_model:
                startActivityForResult(ChooseCarModelActivity.class, 200);
                break;
            case R.id.tv_get_code:
                mvpPresenter.sendCode();
                break;
            case R.id.ll_choose_city:
                startActivityForResult(ChooseCityActivity.class, 300);
                break;
            case R.id.rl_see_to_shop:
                sbSeekCarToggle.toggle();
                break;
            case R.id.rtv_apply:
                mvpPresenter.doApply();
                break;
            case R.id.tv_agreement:
                startActivity(WebActivity.class, new BundleBuilder().putString("url", Constants.YRL_CYHGRXXBHTK).create());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 200:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        CarSeriesInfo carSeriesInfo = (CarSeriesInfo) data.getSerializableExtra("seriesInfo");
                        carChooseInfo = (CarInfo) data.getSerializableExtra("carInfo");
                        if (carSeriesInfo != null && carChooseInfo != null) {
                            tvChooseModelName.setText(carSeriesInfo.getName() + " " + carChooseInfo.getName());
                        }
                    }
                }
                break;
            case 300:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        BaiDuLocationInfo locationInfo = (BaiDuLocationInfo) data.getSerializableExtra("entity");
                        if (locationInfo != null && !StringUtils.isTrimEmpty(locationInfo.getCity())) {
                            chooseCity = locationInfo.getCity();
                            tvPersonCity.setText(chooseCity);
                        }
                    }
                }
                break;
        }
    }

    @Override
    protected LoanQuotaCalculatePresenter createPresenter() {
        return new LoanQuotaCalculatePresenter(this);
    }

    @Override
    public void onSendCodeSuccess(BaseModel<Object> successResult) {
        showToast(String.format(getStringSource(R.string.s_yzmyfs_tips_),
                etPersonPhone.getText().toString().trim()));
        if(phoneLoginTimer == null){
            phoneLoginTimer = new CodeCountDownTimer(tvGetCode, 60000, 1000);
        }
        phoneLoginTimer.start();
        rtvApply.setEnabled(true);
    }

    @Override
    public void onSendCodeFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
        rtvApply.setEnabled(false);
    }

    @Override
    public void onDoApplySuccess(BaseModel<Object> successResult) {
        showToast(successResult.getResultInfo());
        onBackPressed();
    }

    @Override
    public void onDoApplyFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public String getPersonName() {
        return etPersonName.getText().toString().trim();
    }

    @Override
    public String getPersonMobile() {
        return etPersonPhone.getText().toString().trim();
    }

    @Override
    public String getPhoneCode() {
        return etPersonCode.getText().toString().trim();
    }

    @Override
    public String getPersonCity() {
        return chooseCity;
    }

    @Override
    public int getPersonIncome() {
        return personIncome;
    }

    @Override
    public int getPersonSecurity() {
        return personSecurity;
    }

    @Override
    public int getPersonTime() {
        return personTime;
    }

    @Override
    public int getPersonSeeCar() {
        return personSeeCar;
    }

    @Override
    public String getCarModelId() {
        return carChooseInfo != null ? carChooseInfo.getId() : "";
    }

    @Override
    public int getSeeCar() {
        return isSeeCarToShop;
    }

    @Override
    protected void onDestroy() {
        if(phoneLoginTimer != null){
            phoneLoginTimer.release();
            phoneLoginTimer = null;
        }
        super.onDestroy();
    }
}
